package com.utilities.pipelineScripts;

import java.io.PrintWriter;
import com.utilities.IO.fileWriters.PrepareGFF3;
import com.utilities.fastaUtilities.FindGenes;

public class ExtractGeneInfo {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		String gbkFile = args[0];
		String geneFile = args[1];
		String refSeqFile = args[2];
		String geneRangesFile = args[3];
		String refSeqName = args[4];
		String gbkInfoFile = args[5];

		try
		{
			// Find genes and gene ranges in the gbkFile
			FindGenes findGenes = new FindGenes().setInputFile(gbkFile).setOutpuFile(geneFile)
			.setGeneRangesFile(geneRangesFile);
			findGenes.findGenes();
			String seqLength = findGenes.getSeqLength();
			System.out.println("Sequence Length : " + seqLength + " bp");
			System.out.println("Found Genes...");
			System.out.println("Fetching reference sequence from gbkFile");
			// fetching the sequence from the gbkFile
			new PrepareGFF3().setSeqName(refSeqName).fetchSequenceFromGbkFile(refSeqFile, gbkFile);
			
			if (gbkInfoFile != null && !gbkInfoFile.trim().equals(""))
			{
				PrintWriter pw = new PrintWriter(gbkInfoFile);
				pw.println(seqLength);
				pw.flush();
				pw.close();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Error while printing gene ranges");
		}
	}
	
//	private static void findGeneRanges(FindGenes findGenes, String allPossibleGeneRangesFile) throws IOException 
//	{
//		HashMap<String,ArrayList<String>> hashMap = findGenes.getMap();
//		Set<String> keys = hashMap.keySet();
//		ArrayList<Range> list3 = new ArrayList<Range>();
//		for (String key : keys)
//		{
//			ArrayList<String> positionsInfo = hashMap.get(key);
//			for (String positionInfo : positionsInfo)
//			{
//				String[] params = positionInfo.split("\\.\\.");
//				int start = Integer.parseInt(params[0]);
//				int end = Integer.parseInt(params[1]);
//				if (start > end)
//				{
//					int temp = start; start = end; end = temp;
//				}
//				Range range = new Range(start, end, key);
//				list3.add(range);
//			}
//		}
//		Collections.sort(list3, new RangeComparator());
//		FileUtilities.WriteToFile(list3,allPossibleGeneRangesFile);
//	}
}
